<template>
  <a-modal
    :visible="visible"
    :title="t('add')"
    :cancelText="t('cancel')"
    :okText="t('submit')"
    @ok="submit"
    @cancel="cancel()"
  >
    <a-form
      ref="formRef"
      :model="formState"
      :rules="formRules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >     
      <a-form-item ref="login_name" :label="t('login name')" name="login_name">
        <a-input has-feedback v-model:value="formState.login_name" />
      </a-form-item>  

      <a-form-item has-feedback  ref="password" :label="t('password')" name="password">
        <a-input v-model:value="formState.password" type="password"/>
      </a-form-item>
       <a-form-item  has-feedback ref="repeatpassword" :label="t('repeat password')" name="repeatpassword">
        <a-input v-model:value="formState.repeatpassword" type="password"/>
      </a-form-item>
      <a-form-item  ref="user_name" :label="t('username')" name="user_name">
        <a-input v-model:value="formState.user_name" />
      </a-form-item>
       <a-form-item has-feedback ref="phone" :label="t('phone')" name="phone">
        <a-input v-model:value="formState.phone" />
      </a-form-item>
      <a-form-item has-feedback ref="email" :label="t('email')" name="email">
        <a-input v-model:value="formState.email" />
      </a-form-item>   
      <a-form-item :label="t('status')" name="status">
        <a-switch :checkedValue="1" :unCheckedValue="0" :checkedChildren="t('open')" :unCheckedChildren="t('close')" v-model:checked="formState.status" />
     
      </a-form-item>
      <!-- <a-form-item label="备注" name="remark">
        <a-textarea v-model:value="formState.remark" />
      </a-form-item> -->
    </a-form>
  </a-modal>
</template>
<script>
import { message } from "ant-design-vue";
import { useI18n  } from "vue-i18n";
import { add } from "@/core/api/module/user";
import { defineComponent, reactive, ref, toRaw, watch } from "vue";
import { useStore } from 'vuex';
export default defineComponent({
  props: {
    visible: {
      type: Boolean,
    },
  },
  emit: ["close"],
  setup(props, context) {
    const { t } = useI18n();
    const state = reactive({
      depts: [], 
      showPath: true, 
    });

    const formRef = ref();

	  const store = useStore();
    const formState = reactive({ 
      username:"",
      password:"",
      nickname:"",
      status:0,
    });

    let validatePass = async (_rule, value) => {
      if (value === '') {
        return Promise.reject(t('Please input the password'));
      } else {
        if (formState.password !== '') {
          formRef.value.validateFields('repeatpassword');
        }
        return Promise.resolve();
      }
    };
    let validatePass2 = async (_rule, value) => {
      if (value === '') {
        return Promise.reject(t('Please input the password again'));
      } else if (value !== formState.password) {
        return Promise.reject(t("Two inputs don't match!"));
      } else {
        return Promise.resolve();
      }
    };
  
    const formRules = {
      login_name: [{ required: true , message: t('Please input user name'), trigger: "blur" }],
      password: [{ required: true, validator:validatePass,  trigger: "blur" }],  
      repeatpassword: [{ required: true, validator:validatePass2, trigger: "blur" }], 
      phone: [{ required: true, pattern: /^1[3456789]\d{9}$/, message:t('Please input the correct mobile phone number'), trigger: "blur" }], 
      email: [{ required: true, pattern:/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,message:t('Please input the correct email format'), trigger: "blur" }], 
    };

    const submit = (e) => {     

      //console.log(formState.token);
     
      formRef.value
        .validate()
        .then(() => { 
          add(toRaw(formState)).then((response) => { 

            if (response.code == 0) {
              message.success({ content: t('Saved successfully!'), duration: 1 }).then(() => {
                cancel(true);
              });
            } else {
              message.error({ content: response.msg, duration: 1 }).then(() => {
                cancel(true);
              });
            }
          });
        })
        .catch((error) => {
          console.log("error", error);
        });
    };

    const cancel = (e=false) => {
      formRef.value.resetFields();
      context.emit("close", e);
    };
 
   /*
    const loadPower = () => {
      tree({}).then((response) => {
        response.data = [{
          id:0,
          title: "顶级菜单",
          children: response.data
        }]
        state.powers = response.data;
      });
    };
    */


    //loadPower();

    return {
      t,
      state,
      submit,
      cancel,
      formRef,
      formState,
      formRules, 
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },    
    };
  },
});
</script>